package com.springboot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.mapper.DeliveryAddressMapper;
import com.springboot.po.DeliveryAddress;
import com.springboot.service.DeliveryAddressService;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService{
	@Autowired
	DeliveryAddressMapper deliveryAddressMapper;
	
	@Override
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
		List<DeliveryAddress> list = deliveryAddressMapper.listDeliveryAddressByUserId(userId);
		for(DeliveryAddress da : list) {
			if(da.getRemark().equals("1")) {
				list.remove(da);
			}
		}
		return list;
	}

	@Override
	public DeliveryAddress getDeliveryAddressById(Integer daId) {
		DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddressById(daId);
		if(deliveryAddress == null) {
			deliveryAddress = new DeliveryAddress();
			return deliveryAddress;
		}else if(deliveryAddress.getRemark().equals("1")) {
			deliveryAddress = new DeliveryAddress();
			return deliveryAddress;
		}
		return deliveryAddress;
	}

	@Override
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
		return deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
	}
	
	@Transactional
	@Override
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
		deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
		return deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
	}

	@Override
	public int removeDeliveryAddress(Integer daId) {
		DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddressById(daId);
		if(deliveryAddress != null) {
			//判断id合法
			String remark = deliveryAddress.getRemark();
			if(remark.equals("1"))
				//删除过的不能再删除
				return 0;
		}
		return deliveryAddressMapper.removeDeliveryAddress(daId);
	}

}
